package com.bianxianmao.lastknow.fragment;

import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bianxianmao.R;
import com.bianxianmao.base.BaseFragment;
import com.bianxianmao.lastknow.adapter.ListViewAdapter;
import com.bianxianmao.lastknow.adapter.NewAdapter;
import com.bianxianmao.lastknow.bean.LastKnowBean;
import com.bianxianmao.utils.Constants;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/11.
 */

public class NewFragment extends BaseFragment {

    private PullToRefreshListView prlv;

    private Handler handler = new Handler();
    private List<LastKnowBean.ReturnValueBean> bean;
    private List<LastKnowBean.ReturnValueBean> list;
    private PullToRefreshListView pulltorefresh;
    private ListViewAdapter listViewAdapter;


    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.fragment_new, null);

        pulltorefresh = (PullToRefreshListView) view.findViewById(R.id.pulltorefresh);
        NewAdapter newAdapter = new NewAdapter(mContext, bean);
//        listViewAdapter = new ListViewAdapter(mContext, returnValueArray);
        pulltorefresh.setAdapter(newAdapter);
        list = new ArrayList<>();
        initData();
        return view;
    }

    @Override
    public void initData() {
//        final RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        String url = Constants.LASTKNOWURL;
        OkHttpUtils.get().url(url).build().execute(new StringCallback() {

            @Override
            public void onError(com.squareup.okhttp.Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                Toast.makeText(getContext(), "网络连接失败", Toast.LENGTH_SHORT).show();
                Gson gson = new Gson();
                LastKnowBean lastKnowBean = gson.fromJson(response, LastKnowBean.class);
                List<LastKnowBean.ReturnValueBean> data = lastKnowBean.getReturnValue();


                for (int i = 0; i < data.size(); i++) {
                    list.add(data.get(i));

                }
                Log.i("AAACCC", "onResponse: " + list);

            }
        });


    }

}
